﻿using Sai.Core.Reporting;
using System;
using System.Windows.Forms;

namespace Sai.UI
{
    public class ReportFormatCombo : ComboBox
    {
        public ReportFormatCombo()
        {
            this.ReportFormat = ReportFormat.Html;

            this.SelectedIndexChanged += ReportFormatCombo_SelectedIndexChanged;
        }

        void ReportFormatCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReportFormat selectedFormat = ReportFormat.Html;

            foreach (ReportFormat avaiableFormat in Enum.GetValues(typeof(ReportFormat)))
            {
                if (String.Compare(this.Text, avaiableFormat.ToString(), true) == 0)
                {
                    selectedFormat = avaiableFormat;
                    break;
                }
            }
            
            this._format = selectedFormat;
        }

        private void SetSelectedFormat()
        {
            if (this.Items.Count == 0)
                return;

            int selectedIndex = 0;

            foreach (ReportFormat avaiableFormat in Enum.GetValues(typeof(ReportFormat)))
            {
                if (this.ReportFormat == avaiableFormat)
                {
                    this.SelectedIndex = selectedIndex;
                    break;
                }

                ++selectedIndex;
            }
        }

        private ReportFormat _format;

        public ReportFormat ReportFormat 
        {
            get
            {
                return this._format;
            }
            set
            {
                this._format = value;

                this.Items.Clear();

                foreach (ReportFormat availableFormat in Enum.GetValues(typeof(ReportFormat)))
                {
                    this.Items.Add(availableFormat.ToString());
                }

                this.SetSelectedFormat();
            }
        }
    }
}
